package com.qf.controller;

import com.qf.pojo.Msg;
import com.qf.pojo.Orders;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrdersController {
    @Autowired(required = false)
    private OrderService orderService;

    @RequestMapping("/order.do")
    @ResponseBody//展示订单 int getid//已作废
    public Msg order( HttpServletRequest request,Integer getid){
        if(getid==null){
            getid = 1;
        }

        Orders order = orderService.viewOrder(getid);
        Msg msg = new Msg();
        msg.setData(order);
        msg.setCode(1);
        return msg;
    }
    @RequestMapping("/add.do")
    @ResponseBody//订单数据库添加 int getid
    public Msg addOne(int cid,int getid,int backid, int oprice,HttpSession session){
        Orders orders = new Orders();
        Object user1 = session.getAttribute("user");
          Msg m= new Msg<>();
        if (user1 != null && user1 instanceof User){
           User user= (User)user1;
            orders.setCid(cid);
            orders.setUid(user.getId());
            orders.setGetid(getid);
            orders.setBackid(backid);
            orders.setOprice(oprice);
            orders.setStatus("已预订");
            int res = orderService.addOne(orders);
            m.setCode(res);
        }
        return m;

    }

}
